﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PhraseCorrector.SpellLook
{
    public partial class Spell
    {

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 抛出错误信息
        /// </summary>
        /// <param name="maxDictionaryEditDistance">最大字典编辑距离</param>
        /// <param name="prefixLength">前缀长度</param>
        /// <param name="countThreshold">计数阈值</param>
        /// <param name="compactLevel">压缩级别</param>
        public void ThrowError(int maxDictionaryEditDistance, int prefixLength, int countThreshold, byte compactLevel)
        {
            // 检查初始容量、最大字典编辑距离和计数阈值是否为负数
            ThrowNegative(maxDictionaryEditDistance, countThreshold);
            // 检查最大字典编辑距离和前缀长度的关系
            ThrowError(maxDictionaryEditDistance, prefixLength);
            // 检查压缩级别是否超过允许的最大值
            ThrowError(compactLevel);
        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 检查给定的值是否为负数，并抛出异常
        /// </summary>
        /// <param name="maxDictionaryEditDistance">最大字典编辑距离</param>
        /// <param name="countThreshold">计数阈值</param>
        private void ThrowNegative(int maxDictionaryEditDistance, int countThreshold)
        {
            ThrowError(maxDictionaryEditDistance, nameof(maxDictionaryEditDistance));
            ThrowError(countThreshold, nameof(countThreshold));
        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 检查前缀长度是否合法，并根据最大字典编辑距离抛出异常
        /// </summary>
        /// <param name="maxDictionaryEditDistance">最大字典编辑距离</param>
        /// <param name="prefixLength">前缀长度</param>
        private void ThrowError(int maxDictionaryEditDistance, int prefixLength)
        {
            if (prefixLength < 1 || prefixLength <= maxDictionaryEditDistance) throw new ArgumentOutOfRangeException(nameof(prefixLength));
        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 检查压缩级别是否超过允许的最大值，并抛出异常
        /// </summary>
        /// <param name="compactLevel">压缩级别</param>
        private void ThrowError(byte compactLevel)
        {
            if (compactLevel > 16) throw new ArgumentOutOfRangeException(nameof(compactLevel));
        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 检查压缩级别是否小于0，并根据参数名抛出异常
        /// </summary>
        /// <param name="compactLevel">压缩级别</param>
        /// <param name="typename">参数名</param>
        private void ThrowError(int compactLevel, string typename)
        {
            if (compactLevel < 0) throw new ArgumentOutOfRangeException(typename);
        }

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// 初始化配置参数
        /// </summary>
        /// <param name="maxDictionaryEditDistance">最大字典编辑距离</param>
        /// <param name="prefixLength">前缀长度</param>
        /// <param name="countThreshold">计数阈值</param>
        /// <param name="compactLevel">压缩级别</param>
        public void Init(int maxDictionaryEditDistance, int prefixLength, int countThreshold, byte compactLevel)
        {
            this.Wordlibrary= new Dictionary<string, long>();
            this.maxDictionaryEditDistance= maxDictionaryEditDistance;
            this.prefixLength= prefixLength;
            this.countThreshold= countThreshold;
            // 确保压缩级别不超过16
            if (compactLevel > 16) compactLevel= 16;
            this.compactMask= uint.MaxValue >> 3 + compactLevel << 2;
        }
    }
}
